package servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javabean.Factory;
import javabean.user.User;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class UserInfoEditServlet extends BaseServlet{

	private ServletConfig config;
    //初始化Servlet
    final public void init(ServletConfig config)
                throws ServletException{
    this.config=config;
    }
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4194629595765316697L;

	@Override
	protected void service() throws Exception {
		boolean tag = false;
		//获取传过来的信息
		String path = request.getSession().getServletContext().getRealPath("/img/upload");	//临时路径
		String path1 = request.getSession().getServletContext().getRealPath("/img/user");	//存放路径
		String uid = request.getParameter("uid") ;
		if(uid!=null){
				String uname = request.getParameter("uname") ;
				String sex = request.getParameter("sex") ;
				String grade = request.getParameter("grade") ;
				String major = request.getParameter("major");
				String banbie = request.getParameter("banbie") ;
				String tel = request.getParameter("tel") ;
				String shorttel = request.getParameter("shorttel") ;
				String email = request.getParameter("email") ;
				String dormitory = request.getParameter("dormitory") ;
				String birthday = request.getParameter("birthday");
				String introduce = request.getParameter("introduce");
				String picurl = request.getParameter("picurl");
				String oldpicurl = request.getParameter("oldpicurl");
				String isUpload = request.getParameter("isUpload");

				if(!"".equals(uname) && ! "".equals(sex) && ! "".equals(tel) && ! "".equals(email))
				{	
					User user = new User() ;
					user.setUid(uid);
					user.setUname(uname);
					user.setSex(sex);
					user.setGrade(grade);
					user.setMajor(major);
					user.setBanbie(banbie);
					user.setTel(tel);
					user.setShorttel(shorttel);
					user.setEmail(email);
					user.setDormitory(dormitory);
					user.setIntroduce(introduce);
					user.setBirthday(birthday);
					user.setPic(picurl);
					try{
						//更新数据库
						tag = Factory.getUserDAOInstance().edit(user) ;
					}
					catch(Exception e){
						response.getWriter().write("{\"success\":false,\"msg\":\"真不好意思，服务器犯错了，换个姿势试一试\"}");
					}
					if(tag){
						if("true".equals(isUpload)){
							if(copyFile(path,path1,picurl)){
								request.getSession().setAttribute("upic", picurl);
								response.getWriter().write("{\"success\":true,\"msg\":\"修改成功\"}");
							}else{
								response.getWriter().write("{\"success\":true,\"msg\":\"修改成功，图片上传失败了\"}");
							}
							File file = new File(path1,oldpicurl);
							if(file.exists()){
								file.delete();
							}
						}else{
							response.getWriter().write("{\"success\":true,\"msg\":\"修改成功\"}");
						}
						request.getSession().setAttribute("uname", uname);
					}
				}
			}
	}
	
	public boolean copyFile(String path,String path1,String fileUrl){
		boolean tag = false;
		try{
			//复制图片
			int bytesum = 0;
			int byteread = 0;
			File uploadFile = new File(path,fileUrl);
			File newFile = new File(path1,fileUrl);
			if(uploadFile.exists()){
				InputStream inStream = new FileInputStream(uploadFile);
				FileOutputStream fs = new FileOutputStream(newFile);
				byte[] buffer = new byte[ 1444 ];
				while((byteread = inStream.read(buffer)) != -1 ){
					bytesum += byteread; //字节数 文件大小
					fs.write(buffer,0,byteread);
				}
				inStream.close();
				tag  = true;
				uploadFile.delete();
			}
		}catch(Exception ex){
		}
		return tag;
	}
	
}
